package com.yair_srm.action;

//import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yair_srm.action.bean.POList_Sel;
import com.yair_srm.integration.sap.impl.POList_SAP;
import com.yair_srm.model.bean.PurchaseOrder;
import com.yair_srm.model.service.IPOListService;
import com.yair_srm.model.service.impl.POListService;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import java.io.IOException;
import java.util.*;

@SuppressWarnings("serial")
public class POListAction extends ActionSupport implements SessionAware {

	/**
     *
     */
	private Logger log = Logger.getLogger(this.getClass());
	private static final long serialVersionUID = 1L;
	private ArrayList<PurchaseOrder> polist;
	private String total;//total page number
    private String request;//request parameter
    private HashMap req_param;//request parameter hashmap style
    private String start;
    private String limit;
    private IPOListService polistService;
    public static int i = 0;
    
	public String doGetPOList() {

		String vendor = (String) ActionContext.getContext().getSession().get(Action.ID);
        //start of translate page information
		if(start == null){
			start = "0";
		}
		
        int start_i = Integer.parseInt(start);
        start_i = start_i + 1;
        start = Integer.toString(start_i);
	    req_param.put("start", start);
				
//		if(limit == null){
//		    limit = (String)req_param.get("pgsize");
//		}
//		
//	    req_param.put("limit", limit);
		//end of translate page information
	    System.out.println(this.limit);
		//fetch po list through jco
		polist = polistService.getPOList(vendor,req_param);
		System.out.println(polist.size()+"size");
		total = (String) req_param.get("total");
		
		return Action.POLIST;
	}
	    
	public String getRequest() {
		return request;
	}

	public void setRequest(String request) throws JSONException {
		this.request = request;
		req_param = (HashMap) JSONUtil.deserialize(request);
	}

	@JSON(name="polist")
	public ArrayList<PurchaseOrder> getPolist() {
		return polist;
	}

	public void setPolist(ArrayList<PurchaseOrder> polist) {
		this.polist = polist;
	}
	
	@JSON(name="count")
	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}
	

	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
	}

	public void setPolistService(IPOListService service) {
		polistService = service;
	}


}
